﻿// Decompiled with JetBrains decompiler
// Type: LayersParser.Objects.OLPConfiguration
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using LayersParser.Extensions;
using LayersParser.Interfaces;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;


namespace LayersParser.Objects;

public class OLPConfiguration : Common, IConfiguration
{
  private string _controller;
  private XDocument _xDocument = new XDocument();
  private List<Command> _commands = new List<Command>();
  private List<Param> _params = new List<Param>();
  private string _filePath;

  public OLPConfiguration(string name, string controller, string version)
  {
    this.Name = name;
    this._controller = controller;
    this._xDocument = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new object[1]
    {
      (object) new XElement((XName) "RobotController", new object[5]
      {
        (object) new XAttribute((XName) "Name", (object) this._controller),
        (object) new XAttribute((XName) "Version", (object) version),
        (object) new XElement((XName) "RoboticParams"),
        (object) new XElement((XName) "OlpCommands"),
        (object) new XElement((XName) "OlpDialogs")
      })
    });
  }

  public OLPConfiguration(XDocument xDocument, string version)
  {
    XElement root = xDocument.Root;
    List<Command> commandList = new List<Command>();
    if (root.Attribute((XName) "Name") != null)
      this._controller = (string) root.Attribute((XName) "Name");
    if (root.Attribute((XName) "Version") != null)
    {
      if (!root.Attribute((XName) "Version").Value.Contains(version))
      {
        XAttribute xattribute = root.Attribute((XName) "Version");
        xattribute.Value = $"{xattribute.Value},{version}";
      }
    }
    else
      root.Add((object) new XAttribute((XName) "Version", (object) version));
    if (root.Element((XName) "RoboticParams") != null)
    {
      foreach (XElement xelement in root.Element((XName) "RoboticParams").Elements((XName) "Param").ToList<XElement>())
        this._params.Add(new Param(xelement));
    }
    else
      root.Add((object) new XElement((XName) "RoboticParams"));
    if (root.Element((XName) "OlpCommands") != null)
    {
      foreach (XElement commandNode in root.Element((XName) "OlpCommands").Elements((XName) "Command").ToList<XElement>())
      {
        Command command = new Command(commandNode, this._params);
        this._commands.Add(command);
        commandList.Add(command);
      }
    }
    else
      root.Add((object) new XElement((XName) "OlpCommands"));
    if (root.Element((XName) "OlpDialogs") == null)
      root.Add((object) new XElement((XName) "OlpDialogs"));
    this._xDocument = xDocument;
  }

  public string Controller
  {
    get => this._controller;
    set => this._controller = value;
  }

  public List<Command> Commands
  {
    get => this._commands;
    set => this._commands = value;
  }

  public string FilePath
  {
    get => this._filePath;
    set => this._filePath = value;
  }

  public List<Param> Params
  {
    get => this._params;
    set => this._params = value;
  }

  public XDocument XDocument
  {
    get => this._xDocument;
    set => this._xDocument = value;
  }

  public List<ICommand> SharedCommands
  {
    get => this._commands.CommandToICommand();
    set => this._commands = value.ICommandToCommand();
  }
}
